/*
 * Copyright (c) Jiandong.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import LightWeightMap from '@ohos.util.LightWeightMap';
import { ReactShadowNode } from './ReactShadowNode';
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
import HashMap from '@ohos.util.HashMap';


export class ShadowNodeRegistry {
    private mTagsToCSSNodes = new HashMap<number, ReactShadowNode>();
    private mRootTags = new LightWeightMap<number, boolean>();
//    private final SingleThreadAsserter mThreadAsserter = new SingleThreadAsserter();

    public constructor() {
    }

    public addRootNode(node: ReactShadowNode) {
        // this.mThreadAsserter.assertNow();
        let tag: number = node.getReactTag();
        this.mTagsToCSSNodes.set(tag, node);
        this.mRootTags.set(tag, true);
    }

    public removeRootNode(tag: number) {
        // this.mThreadAsserter.assertNow();
        if (tag != -1) {
            if (!this.mRootTags.get(tag)) {
                throw new Error("View with tag " + tag + " is not registered as a root view");
            } else {
                this.mTagsToCSSNodes.remove(tag);
                this.mRootTags.remove(tag);
            }
        }
    }

    public addNode(node: ReactShadowNode) {
        // this.mThreadAsserter.assertNow();
        this.mTagsToCSSNodes.set(node.getReactTag(), node);
    }

    public removeNode(tag: number): void {
        // this.mThreadAsserter.assertNow();
        if (this.mRootTags.get(tag)) {
            throw new Error("Trying to remove root node " + tag + " without using removeRootNode!");
        } else {
            this.mTagsToCSSNodes.remove(tag);
        }
    }

    public getNode(tag: number): ReactShadowNode {
        // this.mThreadAsserter.assertNow();
        return this.mTagsToCSSNodes.get(tag) as ReactShadowNode;
    }

    public isRootNode(tag: number): boolean {
        // // this.mThreadAsserter.assertNow();
        return this.mRootTags.get(tag);
    }

    public getRootNodeCount(): number {
        // this.mThreadAsserter.assertNow();
        return this.mRootTags.length;
    }

    public getRootTag(index: number) {
        // this.mThreadAsserter.assertNow();
         return this.mRootTags.getKeyAt(index);
    }
}
